#include "Memory.h"

void Memory::initialize(){
  uint8_t val;
  uint8_t mem;

  for(byte i = addrModulePiec; i <= addrModuleCircul; i++){
    val = read(i);
    if((val != eepromTrue) && (val != eepromFalse)) write(i, eepromFalse);
  }
  
  for(byte i = addrTempMixKalor; i <= addrTempMixBoiler; i++){
    if((val < 0) || (val > 95)) write(i, 40);
  }
  
  for(byte i = addrHisMixKalor; i <= addrHisKolektor; i++){
    if((val < 0) || (val > 95)) write(i, 10);
  }

  for(uint8_t i = 0; i < 24; i++ ){
    mem = addrBaseKalor + 3 * i;
    val = read(mem);
    if((val != eepromTrue) && (val != eepromFalse)) write(mem, eepromFalse);
    
    mem = addrBaseKalor + 1 + 3 * i;
    val = read(mem);
    if((val < 0) || (val > 95)) write(mem, 25);
    
    mem = addrBaseKalor + 2 + 3 * i;
    val = read(mem);
    if((val < 0) || (val > 10)) write(mem, 1);
  }
  
  for(uint8_t i = 0; i < 8; i++){
    mem = addrBaseKociol + 3 * i;
    val = read(mem);
    if((val != eepromTrue) && (val != eepromFalse)) write(mem, eepromFalse);
    
    mem = addrBaseKociol + 1 + 3 * i;
    val = read(mem);
    if((val < 0) || (val > 95)) write(mem, 80);
    
    mem = addrBaseKociol + 2 + 3 * i;
    val = read(mem);
    if((val < 0) || (val > 10)) write(mem, 1);
  }
  loadSchudleTempKalor();
  loadSchudleTempKociol();
}

void Memory::loadSchudleTempKalor(void){
  for(uint8_t i = 0; i < 24; i++){
      uint8_t mem = addrBaseKalor + 3 * i;
      scheduleTempKalor[i][0] = read(mem);
      scheduleTempKalor[i][1] = read(mem + 1);
      scheduleTempKalor[i][2] = read(mem + 2);
  }
}

void Memory::loadSchudleTempKociol(void){
  for(uint8_t i = 0; i < 8; i++){
      uint8_t mem = addrBaseKociol + 3 * i;
      scheduleTempKociol[i][0] = read(mem);
      scheduleTempKociol[i][1] = read(mem + 1);
      scheduleTempKociol[i][2] = read(mem + 2);
  }
}
  
  
uint8_t Memory::getHisKolektor(void){
  return read(addrHisKolektor);
}

void Memory::setHisKolektor(uint8_t val){
  write(addrHisKolektor, val);
}

uint8_t Memory::getHisMixBoiler(void){
   return read(addrHisMixBoiler);
}

void Memory::setHisMixBoiler(uint8_t val){
   write(addrHisMixBoiler, val);
}

uint8_t Memory::getHisMixKalor(void){
  return read(addrHisMixKalor);
}

void Memory::setHisMixKalor(uint8_t val){
  write(addrHisMixKalor, val);
}

uint8_t Memory::read(uint16_t address){
  return eeprom_read_byte((unsigned char *) address);
}

void Memory::write(uint16_t address, uint8_t value){
  eeprom_write_byte((unsigned char *) address, value);
}

bool Memory::getModulePiec(void){
  uint8_t val = read(addrModulePiec);
  if ( val == eepromTrue ) return true;
  else if( val == eepromFalse )return false;
}

bool Memory::getModuleKolektor(void){
  uint8_t val = read(addrModuleKolektor);
  if ( val == eepromTrue ) return true;
  else if( val == eepromFalse )return false;
}

bool Memory::getModuleGrzejnik(void){
  uint8_t val = read(addrModuleGrzejnik);
  if ( val == eepromTrue ) return true;
  else if( val == eepromFalse )return false;
}

bool Memory::getModuleCircul(void){
  uint8_t val = read(addrModuleCircul);
  if ( val == eepromTrue ) return true;
  else if( val == eepromFalse )return false;
}

void Memory::setModulePiec(bool mod){
  if(mod == true) write(addrModulePiec, eepromTrue);
  else write(addrModulePiec, eepromFalse);
} 

void Memory::setModuleKolektor(bool mod){
  if(mod == true) write(addrModuleKolektor, eepromTrue);
  else write(addrModuleKolektor, eepromFalse);
} 

void Memory::setModuleGrzejnik(bool mod){
  if(mod == true) write(addrModuleGrzejnik, eepromTrue);
  else write(addrModuleGrzejnik, eepromFalse);
}

void Memory::setModuleCircul(bool mod){
  if(mod == true) write(addrModuleCircul, eepromTrue);
  else write(addrModuleCircul, eepromFalse);
}

Memory mem;


